<?php

class ShowControl extends CommonControl {

    public function index() {
        $this->assign_head();
        $this->_cate_info();
        if (IS_POST) {
            $asid = (int) $_POST['ask_id'];
            $cid = (int) $_POST['cid'];
            $uid = (int) $_POST['now_uid'];
            $content = adds_html($_POST['content']);
            if (empty($content)) {
                $this->error('内容不能为空!');
            }
            //用户表某个用户的回答数
            $answer = M('user')->query('SELECT answer FROM hd_user where uid=' . $uid);
            $answer = (int) $answer[0]['answer'] + 1;
            $sql = "UPDATE hd_user set answer={$answer} where uid=" . $uid;
            M('user')->exec($sql);
            //回答的插入
            $sql = "INSERT INTO hd_answer(content,time,uid,asid) values('{$content}'," . time() . ",'{$uid}','{$asid}')";
            $answer = M('category')->exec($sql);
            //问题表的某个问题answer数的插入
            $AnsNum = M('ask')->query('SELECT count(*) as c   FROM hd_answer where  asid=' . $asid);
            $ask_ans = (int) $AnsNum[0]['c'];
            $sql = "UPDATE hd_ask set answer={$ask_ans} where asid=" . $asid;
            M('user')->exec($sql);
            if ($answer) {
                $this->success('回答成功！', __ROOT__ . '?c=show&asid=' . $asid . '&cid=' . $cid);
            }
        }
        //待解决相关问题
        $asid = (int) $_GET['asid'];
        $ncid = (int) $_GET['cid'];
        $sql = 'SELECT * FROM hd_category as c join hd_ask as a on c.cid=a.cid where a.cid=' . $ncid . ' and a.asid!=' . $asid;
        $unsolved = M('category')->query($sql);
        $this->assign('unsolve', $unsolved);
        $sql1 = 'SELECT * FROM hd_ask as a join hd_user as u on a.uid=u.uid where a.asid=' . $asid;
        $data = M('answer')->query($sql1);
        if (empty($data)) {
            //如果用户随意填写asid值，并且asid值取不到内容，则返回首页！
            echo "<script>location.href='" . __ROOT__ . "'</script>";
        }
        $this->assign('askItem', $data[0]);
        //分配问题变量
        $sql2 = 'SELECT *,ha.content as ha, ha.asid as aid,ha.accept as accept,ha.time as time FROM hd_answer as ha join hd_user as hu on ha.uid=hu.uid join hd_ask as has on ha.asid=has.asid where ha.asid=' . $asid . ' order by anid desc';
        $data2 = M('answer')->query($sql2);
        $this->assign('ask_answer', $data2);
        //分配满意答案
        $sql = 'SELECT *,ha.content as con,hu.accept/hu.answer as percent  FROM hd_answer as ha join hd_user as hu on ha.uid=hu.uid join hd_ask as has on ha.asid=has.asid where ha.accept=1 and ha.asid=' . $asid;
        $accept_res = M('user')->query($sql);
        if (!empty($accept_res)) {
            $this->assign('accept_res', $accept_res[0]);
        }
        $sql = 'SELECT *  FROM hd_ask as has join hd_answer as ha on has.asid=ha.asid where has.asid=' . $asid . ' group by has.content order by accept desc ';
//        echo $sql;die;
        $accept = M('ask')->query($sql);
        if ($accept) {
            $this->assign('accept', $accept[0]);
        }
        //分配问题对应的答案变量
        $sql = 'SELECT count(*) as c FROM hd_ask as a join hd_user as u on a.uid=u.uid join hd_answer as ha on a.asid=ha.asid where a.asid=' . $asid;
        $data3 = M('answer')->query($sql);
        $this->assign('answer_num', $data3[0]);
        //分配答案数
        $this->display();
    }

    public function accept() {
        $anid = (int) $_GET['anid'];
        $sql = 'UPDATE hd_answer set accept=accept+1 where anid=' . $anid;
        $sql2 = "update hd_ask set solve=1 where asid=(select asid from hd_answer where anid={$anid})";
        if (M('category')->exec($sql) && M('category')->exec($sql2)) {
            $uid_sql = 'select uid from hd_answer where anid=' . $anid . ' and accept=1';
            $uid = M('category')->query($uid_sql);
            $uid = (int) $uid[0]['uid'];
            $uaccept = 'UPDATE hd_user set accept=accept+1 where uid=' . $uid;
            M('user')->exec($uaccept);
            //采纳成功之后给该用户的accept+1
            $this->error('采纳成功！');
        }
    }

}
